package com.weibo.user.severlet;

import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.weibo.common.BaseSeverlet;
import com.weibo.common.DESCoder;
import com.weibo.common.MailUtil;
import com.weibo.common.Params;
import com.weibo.sql.CommonParm;
import com.weibo.user.User;
import com.weibo.user.service.UserService;
import com.weibo.user.service.imp.UserServiceImp;

public class UserServerlet extends BaseSeverlet {

	private static final Logger logger = Logger.getLogger(UserServerlet.class);

	@Override
	public void doService(HttpServletRequest request,
			HttpServletResponse response) {
		// 得到前台的参数值
		String niname = request.getParameter("niname") == null ? "" : request
				.getParameter("niname");
		String password = request.getParameter("password") == null ? ""
				: request.getParameter("password");
		String url = request.getParameter("url") == null ? "" : request
				.getParameter("url");

		String xingming = request.getParameter("xingming") == null ? ""
				: request.getParameter("xingming");
		String email = request.getParameter("email") == null ? "" : request
				.getParameter("email");
		String yonghushenfen = request.getParameter("yonghushenfen") == null ? ""
				: request.getParameter("yonghushenfen");
		String shouji = request.getParameter("shouji") == null ? "" : request
				.getParameter("shouji");

		// 企业用户
		String gongsi = request.getParameter("gongsi") == null ? "" : request
				.getParameter("gongsi");
		String zhiwei = request.getParameter("zhiwei") == null ? "" : request
				.getParameter("zhiwei");
		String zuoji = request.getParameter("zuoji") == null ? "" : request
				.getParameter("zuoji");

		// 微博主
		String hangye = request.getParameter("hangye") == null ? "" : request
				.getParameter("hangye");
		String address = request.getParameter("address") == null ? "" : request
				.getParameter("address");
		String jiedan_type = request.getParameter("jiedan_type") == null ? ""
				: request.getParameter("jiedan_type");
		String jiedan_time = request.getParameter("jiedan_time") == null ? ""
				: request.getParameter("jiedan_time");
		String starttime = request.getParameter("starttime") == null ? ""
				: request.getParameter("starttime");
		String endtime = request.getParameter("endtime") == null ? "" : request
				.getParameter("endtime");

		try {
			password = DESCoder.encryptBASE64(DESCoder.encrypt(
					DESCoder.decryptBASE64(password), Params.VALIDATE_KEY));
		} catch (Exception e1) {
			logger.error("加密异常！");
			e1.printStackTrace();
		}

		// 接受的前台数据对应处理
		HttpSession session = request.getSession();
		// 初始化model，service，进行对应保存
		User user = new User();
		UserService userService = new UserServiceImp();
		boolean flag = false;
		user.setUser_name(niname);
		user.setUser_psw(password);
		user.setUser_weibo_path(url);
		user.setUser_rename(xingming);
		user.setUser_mail(email);
		user.setUser_phone(shouji);
		user.setUser_state("no");
		if (yonghushenfen.equals("1")) {
			user.setUser_comppany(gongsi);
			user.setUser_duty(zhiwei);
			user.setUser_telphone(zuoji);
			user.setUser_type("1");
			if (!niname .equals("") && !password .equals("") && !url.equals("") && !email.equals("")
					&& !gongsi.equals("")) {
				flag = true;
			}
		} else if (yonghushenfen.equals("2")) {
			user.setUser_duty_sort(hangye);
			user.setUser_city(address);
			user.setUser_order_sort(jiedan_type);
			user.setUser_order_time(jiedan_time);
			user.setUser_type("1");
			user.setUser_start_time(starttime);
			user.setUser_end_time(endtime);
			if (!niname.equals("")  && !password .equals("") && !url.equals("") && !email.equals("")) {
				flag = true;
			}
		}

		if (flag) {
			flag = userService.saveUser(user);
		}
		// 注册成功
		if (flag) {
			logger.info(niname + "用户注册成功！");

			String registerId = "" + Math.random() * Math.random();

			String basepath = request.getScheme() + "://"
					+ request.getServerName() + ":" + request.getServerPort()
					+ getServletContext().getContextPath() + "/";
			String registerurl = basepath + "MailBackServlet.do?registerId="
					+ registerId;// 待会用户点在邮箱中点击这个链接回到你的网站。

			session.setAttribute(registerId, niname);
			session.setMaxInactiveInterval(600);
			logger.info("发送验证邮件！");
			MailUtil.sendMail(email, registerurl);
			try {
				request.setAttribute("email", email);
				request.getRequestDispatcher("/WEB-INF/login/Login_success.jsp")
						.forward(request, response);
			} catch (ServletException e) {
				logger.error("UserServerlet ServletException");
				e.printStackTrace();
			} catch (IOException e) {
				logger.error("UserServerlet IOException");
				e.printStackTrace();
			}
		} else {
			try {
				request.getRequestDispatcher("/Login_business.jsp").forward(
						request, response);
			} catch (ServletException e) {
				logger.error("UserServerlet ServletException");
				e.printStackTrace();
			} catch (IOException e) {
				logger.error("UserServerlet IOException");
				e.printStackTrace();
			}
		}
	}

}
